package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.servrce.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.message.callback.PrivateKeyCallback;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session){
        log.info("用户开始登录了,登录信息:"+employee);

        //1: 获取到 页面传递用户名和密码
        String username = employee.getUsername();
        String password = employee.getPassword();
        //2: 将密码进行md5加密
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        //3: 根据用户名 去 查询 用户信息
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        // 找service  service有没有根据用户名去查询呢?
        // T getOne(Wrapper<T> queryWrapper);
        // select * from employee where username = ?;
        //查询
        //4: 不存在这个用户 返回登录失败
        wrapper.eq(Employee::getUsername,username);
        //5: 存在 就使用 查询出来的密码和 录入加密后密码  密码比对
        // 比对结果是false  返回登录失败
        Employee one = employeeService.getOne(wrapper);
        if (one==null){
            return R.error("用户名和密码不一致");
        }
        if (!one.getPassword().equals(password)){
            return R.error("用户名和密码不一致");
        }
        if (one.getStatus()==0){
            return R.error("账号已被封禁，请与管理员联系");
        }
        session.setAttribute("employee",one.getId());
        return R.success(one);

        //6: 比对结果是正确的 查看 员工的状态
        //  如果是 0  说明被禁用了  提示找管理员 结束登录
        //7: 是1 可以登录  就要把用户id存到 session中
        //8:返回结果

//     return  new R(1,new Employee());
//        return R.success(new Employee());
//     return  new R(0,"失败");
//        return R.error("登录失败");

    }
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        session.removeAttribute("employee");
        session.invalidate();
        return R.success("推出成功");
    }
// 1.打印请求数据
//2.设置初始密码(MD5加密)
//3.设置创建和更新时间
//4.获取员工ID，并设置创建、更新用户Id
//5.完成保存业务
    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpSession session){
        log.info("开始添加");
        log.info("当前经过 save方法 线程id"+Thread.currentThread().getId());
        String password=DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
//        LocalDateTime dateTime = LocalDateTime.now();
//        employee.setCreateTime(dateTime);
//        employee.setUpdateTime(dateTime);
//        Long empID=(Long)session.getAttribute("employee");
//        employee.setCreateUser(empID);
//        employee.setUpdateUser(empID);
        employeeService.save(employee);
        return R.success("添加成功");
    }
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){
//        1.构造分页构造器
        Page pageInfo = new Page(page, pageSize);

//        2.构造条件构造器
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();

//        3.添加过滤条件
        if (name!=null&&name.length()>0){

            wrapper.like(Employee::getUsername,name);
        }
//        4.添加排序条件
        wrapper.orderByDesc(Employee::getUpdateTime);
//        5. 执行查询
        employeeService.page(pageInfo,wrapper);
        return R.success(pageInfo);

    }
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpSession session){
        log.info("当前经过update方法线程id"+Thread.currentThread().getId());
        Long empId=(Long) session.getAttribute("employee");
//        employee.setUpdateUser(empId);
//        LocalDateTime dateTime = LocalDateTime.now();
//        employee.setUpdateTime(dateTime);
        employeeService.updateById(employee);
        return R.success("修改成功");
    }
    @GetMapping
    public R<Employee>getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if (employee==null){
            return R.error("兄弟该ID对应的用户已经找不到了");
        }
        return R.success(employee);
    }
}
